{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Root element : New Arrivals\n",
      "*****Movie*****\n",
      "Title: Enemy Behind\n",
      "Type: War, Thriller\n",
      "Format: DVD\n",
      "Rating: PG\n",
      "Description: Talk about a US-Japan war\n",
      "*****Movie*****\n",
      "Title: Transformers\n",
      "Type: Anime, Science Fiction\n",
      "Format: DVD\n",
      "Rating: R\n",
      "Description: A schientific fiction\n",
      "*****Movie*****\n",
      "Title: Trigun\n",
      "Type: Anime, Action\n",
      "Format: DVD\n",
      "Rating: PG\n",
      "Description: Vash the Stampede!\n",
      "*****Movie*****\n",
      "Title: Ishtar\n",
      "Type: Comedy\n",
      "Format: VHS\n",
      "Rating: PG\n",
      "Description: Viewable boredom\n"
     ]
    }
   ],
   "source": [
    "#!/usr/bin/python\n",
    "# -*- coding: UTF-8 -*-\n",
    " \n",
    "from xml.dom.minidom import parse\n",
    "import xml.dom.minidom\n",
    " \n",
    "# 使用minidom解析器打开 XML 文档\n",
    "DOMTree = xml.dom.minidom.parse(\"movies.xml\")\n",
    "collection = DOMTree.documentElement\n",
    "if collection.hasAttribute(\"shelf\"):\n",
    "   print \"Root element : %s\" % collection.getAttribute(\"shelf\")\n",
    " \n",
    "# 在集合中获取所有电影\n",
    "movies = collection.getElementsByTagName(\"movie\")\n",
    " \n",
    "# 打印每部电影的详细信息\n",
    "for movie in movies:\n",
    "   print \"*****Movie*****\"\n",
    "   if movie.hasAttribute(\"title\"):\n",
    "      print \"Title: %s\" % movie.getAttribute(\"title\")\n",
    " \n",
    "   type = movie.getElementsByTagName('type')[0]\n",
    "   print \"Type: %s\" % type.childNodes[0].data\n",
    "   format = movie.getElementsByTagName('format')[0]\n",
    "   print \"Format: %s\" % format.childNodes[0].data\n",
    "   rating = movie.getElementsByTagName('rating')[0]\n",
    "   print \"Rating: %s\" % rating.childNodes[0].data\n",
    "   description = movie.getElementsByTagName('description')[0]\n",
    "   print \"Description: %s\" % description.childNodes[0].data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.15"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
